package com.xcc.modules.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xcc.common.constant.SystemConstant;
import com.xcc.common.entry.User;
import com.xcc.common.result.ResultBody;
import com.xcc.common.service.UserService;
import com.xcc.common.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * @name: frosty
 * @description : 用户
 * @date: 2019/11/8
 */

@RestController
public class UserController {

    @Autowired
    private  RedisUtil redisUtil;

    @Value("${server.port}")
    private int port;
    @Autowired
    private   UserService userService;
    @PostMapping("/register")
    public ResultBody register(User user){
        user.setId(IdWorker.getIdStr());
        boolean save = userService.save(user);
        return  ResultBody.success(port);
    }
    @GetMapping("/list")
    public  ResultBody list(@RequestParam(required = false) Map<String ,Object> map){
        return  userService.listByPage(map);
    }

    @GetMapping("/find/{id}")
    public ResultBody find(@PathVariable("id") String id ){

        if(StringUtils.checkValNull(id)){
            return  ResultBody.error("id 不能为空");
        }
        return  ResultBody.success(userService.getById(id));
    }


    @PostMapping("/update")
    public  ResultBody update(User user){
    if(StringUtils.checkValNull(user.getId())){
        return  ResultBody.error("id not is null");
    }
         boolean updateById = userService.updateById(user);
       if(updateById){
           return  ResultBody.success();
       }else {
           return ResultBody.error("update  is failed");
       }

    }

    @GetMapping("/delete/{id}")
    public  ResultBody delete(@PathVariable("id")String id){
        if(StringUtils.checkValNull(id)){
            return  ResultBody.error("id not is null");
        }
         boolean b = userService.removeById(id);
        if(b){
            return  ResultBody.success();
        }else {
            return ResultBody.error("delete is failed");
        }
    }
        @GetMapping("/login")
    public  ResultBody login(String username,String password){
        return  userService.login(username,password);
 }

}

